$(document).ready(function(){

	$("#test").hide();
	$("#dob_day1").hide();
	$("#dob_day2").hide();
	$('#dob_day').attr("disabled", true);
	$('#terms-content').hide();
	$('#terms-title').click(function(){
		$('#terms-content').slideToggle()
		return false
	})
	//hide first the error notes
	$('.note.error').hide();
	$('#de-pack').hide();
	$('#help-icon').mouseover(function(){
		$('#d-help').show();
	})
	$('#help-icon').mouseout(function(){
		$('#d-help').hide();
	})

	$('#upgrade-checkout-okay').live('click',function(){
		var base_path_u = $('#base_path_u').html();
		var form_data = $('#upgrade_form').serialize();
		$.ajax({
			url: base_path_u+"register/upgradeAcnt",
			data: form_data,
			type : 'POST',
			success: function(response){
				document.location.href = base_path_u;
			}
		});
		return false;
	});

	$('#upgrade-checkout-pay').live('click',function(){
		var base_path_u = $('#base_path_u').html();
		var form_data = $('#upgrade_form').serialize();
		$.ajax({
			url: base_path_u+"register/saveUpgrade",
			data: form_data,
			type : 'POST',
			success: function(response){
				$("#main-content-wrapper").html(response);
			}
		});
		return false;
	});

	$('#a-ok').live('click',function(){
		var baseurl = $('#baseurl').attr("value");
		if(!baseurl){
			baseurl = $('#base_path_u').html();
		}
		var origvipprice = $('#origvipprice').attr("value");
		if((packcode = $('#i-packcode').val()) == '' ){
			$("#de-pack").hide();
			$("#er-pack").hide();
			$("#prod-price").html(origvipprice+'.00');
			$("#upgrade-checkout-okay").hide();
			$("#upgrade-checkout-pay").show();
			return false;
		}
		$.ajax({
			type: "POST",
			dataType: "json",
			url: baseurl+"register/packagecode_valid",
			data: 'package_code='+packcode,
			success: function(data){
				if(data.is_valid){
					$("#er-pack").hide();
					$("#code_validate").val('valid');
					$("#reseller-name").html(data.owner);
					$("#prod-price").html('0.00');
					$("#de-pack").show();
					$("#upgrade-checkout-pay").hide();
					$("#upgrade-checkout-okay").show();
				}else{
					$("#prod-price").html(origvipprice+'.00');
					$("#de-pack").hide();
					$("#code_validate").val('invalid');
					$("#er-pack").show();
					$("#upgrade-checkout-okay").hide();
					$("#upgrade-checkout-pay").show();
				}
			}
		});
		return false;
	})

	/* validation for sign-up */
	$('#reg-submit, #reg-checkout-submit').live('click',function(){
		var baseurl = $('#baseurl').attr("value");
		var fname = $('#reg-first-name').attr("value");
		var lname = $('#reg-last-name').attr("value");
		var mname = $('#reg-middle-name').attr("value");
		var email = $('#reg-email').attr("value");
		var c_email = $('#reg-re-email').attr("value");
		var password = $('#reg-password').attr("value");
		var re_password = $('#reg-re-password').attr("value");
		var dob = $('#reg-dob').attr("value");
		var captcha = $('#captcha').attr("value");
		var type = $('#register input:radio:checked').val();
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var error = true;
		$("span .reg_error").remove();
		$("span .reg_check").remove();
		if(fname == ""){
			$("#de-fn").hide();
			$("#er-fn").show();
		}else{
			$("#de-fn").show();
			$("#er-fn").hide();
		}
		if(lname == ""){
			$("#de-ln").hide();
			$("#er-ln").show();
		}else{
			$("#de-ln").show();
			$("#er-ln").hide();
		}
		if(mname == ""){
			$("#de-mn").hide();
			$("#er-mn").show();
		}else{
			$("#de-mn").show();
			$("#er-mn").hide();
		}
		if(email == ""){
			$("#de-ea").hide();
			$("#er-ea").html("Please specify an Email Address");
			$("#er-ea").show();
		}else if(!(emailReg.test(email)) ){
			$("#de-ea").hide();
			$("#er-ea").html("Invalid Email Address");
			$("#er-ea").show();
		}else{
			$.ajax({
				type: "POST",
				url: baseurl+"register/email_exist",
				data: 'reg-email='+email,
				success: function(data){
					if(data=='true'){
						$("#email_validate").val(0);
						$("#de-ea").hide();
						$("#er-ea").html("Email already exist.");
						$("#er-ea").show();
						//$("#register #e-mail").append("<div class = 'reg_error'>Email already exist.</div>");
					}else{
						$("#de-ea").show();
						$("#er-ea").hide();
						$("#email_validate").val(1);
						//$("#register #e-mail").append("<div class = 'reg_check'>Valid Email Address</div>");
					}
				}
			});
		}
		if(c_email == ""){
			$('#de-cea').hide();
			$('#er-cea').show();
		}else if(email != c_email){
			$('#de-cea').hide();
			$("#er-cea").html("Email address do not match.");
			$("#er-cea").show();
		}else{
			$('#de-cea').show();
			$("#er-cea").hide();
		}
		if(password == ""){
			$("#de-pa").hide();
			$("#er-pa").html("Please specify password and must be 5 characters long.");
			$("#er-pa").show();
		}else if(password.length < 6){
			$("#de-pa").hide();
			$("#er-pa").html("Must be 5 characters long.");
			$("#er-pa").show();
		}else {
			$("#de-pa").show();
			$("#er-pa").hide();
		}
		if(re_password == ""){
			$("#de-cp").hide();
			$("#er-cp").html("Please fill up confirm password.");
			$("#er-cp").show();
		}else if(re_password != password){
			$("#de-cp").hide();
			$("#er-cp").html("Password do not match.");
			$("#er-cp").show();
		}else{
			$("#de-cp").show();
			$("#er-cp").hide();
		}
		//if($('#register input:radio:checked').length == 0){
		//	$("#register #r-type").append("<div class = 'reg_error'>Please choose type of account</div>");
		//}else{
		//	$("#register #r-type").append("<div class = 'reg_check'>Type of account</div>");
		//	$('#register input:radio:checked').attr('checked','checked');
		//}
		// for checkbox
		if($("#register #checkbox").is(":checked")){
			$("#er-te").hide();
		}else{
			$("#er-te").show();
		}
		// for captcha
		if(captcha == ""){
			$("#de-ca").hide();
			$("#er-ca").show();
		}else{
			var $captcha = $('#captcha').val();
			$.ajax({
				url:baseurl+'register/captcha_validate',
				type:'post',
				data:'captcha='+$captcha,
				success:function(data) {
					if(data=="same"){
						$('#captcha_validate').val(1);
						$("#de-ca").show();
						$("#er-ca").hide();
						if($("div#register .error:visible").length == 0 && $("#captcha_validate").val() == 1 && $("#email_validate").val() == 1){
							$('#frm_register').submit();
						}
					}else{
						$("#de-ca").hide();
						$("#er-ca").show();
					}
				}
			});
		}
		return false;
	});

	});
